﻿module WingBeats.Xhtml.XhtmlElement.Tests
open   Xunit
open   WingBeats
open   WingBeats.Xhtml
open   WingBeats.Xml

type TestAttribute = FactAttribute

let isEqualTo expected actual =
    Assert.Equal(expected, actual)

let e = XhtmlElement()

[<Test>]
let ``<Create self closing element without attributes>`` () =
    e.Br()
    |> isEqualTo (SelfClosingNode ("br", []))

[<Test>]
let ``<Create self closing element with attributes using CSS Creators>`` () =
    e.Img ".foo[src='thing.jpg';alt='a thing']"
    |> isEqualTo (SelfClosingNode("img", [("class", "foo"); ("src", "thing.jpg"); ("alt", "a thing")]))

[<Test>]
let ``<Create self closing element with attributes using statically checked notation>`` () =
    SelfClosingNode("fail", []) // e.Img - don't know the notation yet
    |> isEqualTo (SelfClosingNode ("img", []))

[<Test>]
let ``<Create empty tag pair element without attributes>`` () =
    e.Div e.Empty
    |> isEqualTo (TagPairNode ("div", [], []))

[<Test>]
let ``<Create empty tag pair element with attributes using CSS Creators>`` () =
    e.A "#my-id.my-class.my-class2[ href=http://www.example.com; title=This is a title]" []
    |> isEqualTo 
        (TagPairNode ("a", [("id", "my-id"); ("class", "my-class my-class2"); ("href", "http://www.example.com"); ("title", "This is a title")], []))

[<Test>]
let ``<Create tag pair element with content and attributes using CSS Creators>`` () =
    e.A ".my-link[href=http://www.example.com]"
        [ e.Span [ e.Str "my span" ] ]
    |> isEqualTo 
        (TagPairNode ("a", [ ("class", "my-link"); ("href", "http://www.example.com") ],
            [ TagPairNode ("span", [], [ Text "my span" ]) ] ))

[<Test>]
let ``<Create tag pair element with content and attributes using statically checked notation>`` () =
    TagPairNode ("fail", [], [e.Span [ e.Str "my span" ]]) // Statically checked notation not developed yet
    |> isEqualTo
        (TagPairNode ("div", [("align", "center")], 
            [ TagPairNode ("span", [], [ Text "my span" ]) ] ))

[<Test>]
let ``<Create empty tag pair element with attributes using statically checked notation>`` () =
    TagPairNode ("fail", [], []) // Statically checked notation not developed yet
    |> isEqualTo (TagPairNode ("div", [("align", "center")], []))

[<Test>]
let ``<Add attributes using CSS Creators>`` () =
    e.A "[href=http://www.example.com]" [ e.Str "title text" ]
    |> isEqualTo (TagPairNode ("a", [ ("href", "http://www.example.com") ], [ Text "title text" ]))

[<Test>]
let ``<Add attributes and class using CSS Creators>`` () =
    e.A ".test-class[href=http://www.example.com]" [ e.Str "title text" ]
    |> isEqualTo (TagPairNode ("a", [ ("class", "test-class"); ("href", "http://www.example.com") ], [ Text "title text" ]))

[<Test>]
let ``<Create node hierarchy with CSS Creators>`` () =
    e.Div ".foo > p + br + div > h1.bar + p#thing" []
    |> isEqualTo  
          (TagPairNode
            ("div",[("class", "foo")],
             seq
               [TagPairNode ("p",[],[]); SelfClosingNode ("br",[]);
                TagPairNode
                  ("div",[],
                   [TagPairNode ("h1",[("class", "bar")],[]);
                    TagPairNode ("p",[("id", "thing")],[])])]))

[<Test>]
let ``<Create PlaceHolders with CSS Creators>`` () =
    e.Html ".my > head$head-content + body$body-content" []
    |> isEqualTo
        (TagPairNode
            ("html", [("class", "my")],
                seq [ TagPairNode ("head", [], [ Placeholder ("head-content") ])
                      TagPairNode ("body", [], [ Placeholder ("body-content") ]) ] ))

[<Test>]
let ``<Create children via CSS Creators>`` () =
    e.Div "> h1#id + .text" []
    |> isEqualTo
          (TagPairNode
            ("div",[],
             seq
               [TagPairNode ("h1",[("id", "id")],[]);
                TagPairNode ("div",[("class", "text")],[])]))
 
[<Test>]
let ``<Create hierarchy using creator string that begins with a combinator, and literal children>`` () =
    e.Html "> head > title" [e.Body e.Empty]
    |> isEqualTo
          (TagPairNode
            ("html",[],
             seq
               [TagPairNode ("head",[],[TagPairNode ("title",[],[])]);
                TagPairNode ("body",[],seq [])]))